﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Felbot.Interaction;

namespace Felbot {
    /// <summary>A little form that allows the user to store and use a list of CD-keys.</summary>
    public partial class frmFlagsManager : Form {
        int m_Act = 0;
        frmUserEntryManager m_ManagerEntry;
        XmlEntry m_Entry;
        frmCommandManager m_ManagerCmd;
        XmlCommand m_Command;
        List<string> m_Flags = new List<string>();

        /// <summary>Creates a new flags form.</summary>
        /// <param name="manager">The User Entry Manager that opened it.</param>
        /// <param name="entry">The entry.</param>
        public frmFlagsManager(frmUserEntryManager manager, XmlEntry entry) {
            InitializeComponent();
            m_ManagerEntry = manager;
            m_Entry = entry;
            m_Act = 1;
            lvFlags.CheckBoxes = true;
            LoadFlagsList();
        }

        /// <summary>Creates a new flags form.</summary>
        /// <param name="manager">The Command Manager that opened it.</param>
        /// <param name="command">The command.</param>
        public frmFlagsManager(frmCommandManager manager, XmlCommand command) {
            InitializeComponent();
            m_ManagerCmd = manager;
            m_Command = command;
            m_Act = 2;
            lvFlags.CheckBoxes = true;
            LoadFlagsList();
        }

        /// <summary>Creates a new flags form.</summary>
        public frmFlagsManager() {
            InitializeComponent();
            m_Act = 0;
            lvFlags.CheckBoxes = false;
            LoadFlagsList();
        }

        private void LoadFlagsList() {
            List<string> checkedFlags;
            foreach (XmlFlag flag in CommandControl.FlagsList) {
                ListViewItem item = lvFlags.Items.Add(new ListViewItem(flag.Name));
                item.ToolTipText = flag.Description;
                switch (m_Act) {
                    default:
                    case 0:
                        continue;
                    case 1:
                        checkedFlags = m_Entry.Has.Flags;
                        break;
                    case 2:
                        checkedFlags = m_Command.Requires.Flags;
                        break;
                }
                if (checkedFlags.Contains(flag.Name)) {
                    item.Checked = true;
                }
            }
        }

        private void frmFlagsManager_FormClosing(object sender, FormClosingEventArgs e) {
            List<XmlFlag> flags = new List<XmlFlag>();
            XmlFlag flag;
            foreach (ListViewItem item in lvFlags.Items){
                flag = new XmlFlag();
                flag.Name = item.Text;
                flag.Description = item.ToolTipText;
            }
            CommandControl.FlagsList = flags;
            CommandControl.Save();

            if (m_Act != 0) {
                List<string> flagschecked = new List<string>();
                foreach (ListViewItem item in lvFlags.Items) {
                    if (item.Checked)
                        flagschecked.Add(item.Text);
                }

                switch (m_Act) {
                    case 1:
                        m_ManagerEntry.SetFlags(flagschecked);
                        break;
                    case 2:
                        m_ManagerCmd.SetFlags(flagschecked);
                        break;
                }
            }
        }

        private void lvKeys_SelectedIndexChanged(object sender, EventArgs e) {
            btnEdit.Enabled = (lvFlags.SelectedItems.Count == 1);
            btnRemove.Enabled = (lvFlags.SelectedItems.Count > 0);
        }


        private void btnAdd_Click(object sender, EventArgs e) {
            frmFlagEditor editor = new frmFlagEditor("", "");
            if (editor.ShowDialog() == DialogResult.OK) {
                lvFlags.Items.Add(editor.m_Flag, editor.m_Flag, editor.m_Description);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e) {
            if (lvFlags.SelectedItems.Count == 1) {
                frmFlagEditor m_frmEditor = new frmFlagEditor(lvFlags.SelectedItems[0].Text, lvFlags.SelectedItems[0].ToolTipText);
                if (m_frmEditor.ShowDialog() == DialogResult.OK) {
                    int index = lvFlags.SelectedItems[0].Index;
                    lvFlags.Items.Remove(lvFlags.SelectedItems[0]);
                    lvFlags.Items.Insert(index, m_frmEditor.m_Flag, m_frmEditor.m_Flag, m_frmEditor.m_Description);
                }
            }
        }

        private void btnRemove_Click(object sender, EventArgs e) {
            foreach (ListViewItem item in lvFlags.SelectedItems) {
                lvFlags.Items.Remove(item);
            }
        }

        private void btnDone_Click(object sender, EventArgs e) {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private class frmFlagEditor : Form {
            public string m_Flag;
            public string m_Description;

            public frmFlagEditor(string flag, string description) {
                InitializeComponent();
                m_Flag = flag;
                m_Description = description;
            }

            /// <summary>
            /// Required designer variable.
            /// </summary>
            private IContainer components = null;

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing) {
                if (disposing && (components != null)) {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

            #region Windows Form Designer generated code

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent() {
                this.components = new Container();
                this.btnOK = new Button();
                this.btnCancel = new Button();
                this.txtFlag = new TextBox();
                this.txtDescription = new TextBox();
                this.SuspendLayout();
                // 
                // btnOK
                // 
                this.btnOK.Anchor = ((AnchorStyles) ((AnchorStyles.Top | AnchorStyles.Right)));
                this.btnOK.DialogResult = DialogResult.OK;
                this.btnOK.Location = new Point(200, 6);
                this.btnOK.Name = "btnOK";
                this.btnOK.Size = new Size(112, 23);
                this.btnOK.TabIndex = 3;
                this.btnOK.Text = "OK";
                this.btnOK.UseVisualStyleBackColor = true;
                this.btnOK.Click += new EventHandler(btnOK_Click);
                // 
                // btnCancel
                // 
                this.btnCancel.Anchor = ((AnchorStyles) ((AnchorStyles.Top | AnchorStyles.Right)));
                this.btnCancel.DialogResult = DialogResult.Cancel;
                this.btnCancel.Location = new Point(200, 35);
                this.btnCancel.Name = "btnCancel";
                this.btnCancel.Size = new Size(112, 23);
                this.btnCancel.TabIndex = 2;
                this.btnCancel.Text = "Cancel";
                this.btnCancel.UseVisualStyleBackColor = true;
                this.btnCancel.Click += new EventHandler(btnCancel_Click);
                // 
                // txtKey
                // 
                this.txtFlag.Anchor = ((AnchorStyles) ((AnchorStyles.Top | AnchorStyles.Left)));
                this.txtFlag.Location = new Point(6, 6);
                this.txtFlag.Name = "txtFlag";
                this.txtFlag.Size = new Size(182, 22);
                this.txtFlag.TabIndex = 0;
                // 
                // cboClient
                // 
                this.txtDescription.Anchor = ((AnchorStyles) ((AnchorStyles.Top | AnchorStyles.Left)));
                this.txtDescription.Location = new Point(6, 35);
                this.txtDescription.Name = "txtDescription";
                this.txtDescription.Size = new Size(182, 22);
                this.txtDescription.TabIndex = 1;
                // 
                // frmCDKeyManager
                // 
                this.AcceptButton = this.btnOK;
                this.AutoScaleDimensions = new SizeF(6F, 13F);
                this.AutoScaleMode = AutoScaleMode.Font;
                this.CancelButton = this.btnCancel;
                this.Controls.Add(this.btnOK);
                this.Controls.Add(this.btnCancel);
                this.Controls.Add(this.txtFlag);
                this.Controls.Add(this.txtDescription);
                this.ClientSize = new Size(324, 85);
                this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.Name = "frmFlagEditor";
                this.ShowIcon = false;
                this.ShowInTaskbar = false;
                this.Text = "Flag Editor";
                this.TopMost = true;
                this.Load += new EventHandler(frmFlagEditor_Load);
                this.ResumeLayout(false);

            }

            private Button btnOK;
            private Button btnCancel;
            private TextBox txtFlag;
            private TextBox txtDescription;

            #endregion

            private void frmFlagEditor_Load(object sender, EventArgs e) {
                txtFlag.Text = m_Flag;
                txtDescription.Text = m_Description;
            }

            void btnCancel_Click(object sender, EventArgs e) {
                this.DialogResult = btnCancel.DialogResult;
                this.Close();
            }

            void btnOK_Click(object sender, EventArgs e) {
                this.DialogResult = btnOK.DialogResult;
                this.Close();
            }
        }
    }
}
